package com.customization.api.e9sso.cmd;

import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;
import com.customization.commons.Console;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;

import com.engine.workflow.biz.freeNode.FreeNodeBiz;
import com.engine.workflow.biz.publicApi.RequestOperateBiz;
import com.engine.workflow.biz.requestSubmit.RequestForwardBiz;
import com.engine.workflow.constant.PAResponseCode;
import com.engine.workflow.entity.publicApi.PAResponseEntity;
import com.engine.workflow.entity.publicApi.ReqOperateRequestEntity;

import com.engine.workflow.publicApi.WorkflowRequestOperatePA;
import com.engine.workflow.publicApi.impl.WorkflowRequestOperatePAImpl;
import com.engine.workflow.util.CommonUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.workflow.webservices.WorkflowRequestTableField;

import java.util.*;

public class ForwardRequestCmd extends AbstractCommonCommand<Map<String,Object>> {


    public ForwardRequestCmd(User user, Map<String, Object> params) {
        this.user = user;
        this.params = params;

    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        String uuid=UUID.randomUUID().toString();
        Console.log(uuid+"输入参数params:"+params.toString());

        Map<String, Object> apidatas = new HashMap<String, Object>();
        apidatas.put("status", false);
        apidatas.put("msg", "未查询到相关数据");
        JSONArray json = new JSONArray();
        RecordSet rs = new RecordSet();
        int requestId = Util.getIntValue(Util.null2String(params.get("requestId")),0);
        String forwardWorkcode = Util.null2String(params.get("forwardWorkcode"));
        String forwardResourceId="";


        if (requestId==0||forwardWorkcode.equals("")) {
            apidatas.put("status", false);
            apidatas.put("msg", "requestId、forwardWorkcode不能为空");
            Console.log(uuid+"返回值:"+apidatas.toString());
            return apidatas;
        }
        rs.execute("select * from hrmresource where workcode ='"+forwardWorkcode+"'");
        if(rs.next()){
            forwardResourceId=rs.getString("id");
        }else{
            apidatas.put("status", false);
            apidatas.put("msg", "未找到员工号为"+forwardWorkcode +"的员工;");
            Console.log(uuid+"返回值:"+apidatas.toString());
            return apidatas;
        }


        try {

            WorkflowRequestOperatePA workflowRequestOperatePA = new WorkflowRequestOperatePAImpl();

            int forwardFlag=3;

            Console.log("requestId:"+requestId);
            Console.log("forwardFlag:"+forwardFlag);
            Console.log("forwardResourceId:"+forwardResourceId);
            Console.log("this.user:"+this.user.getLastname());
            Map<String,Object> param=new HashMap<>();
            param.put("remark",Util.null2String(params.get("remark")));

            RequestForwardBiz requestForwardBiz = new RequestForwardBiz(requestId, forwardFlag, forwardResourceId, this.user, param);
            PAResponseCode paResponseCode = requestForwardBiz.execute();
            if (paResponseCode != PAResponseCode.SUCCESS) {
                apidatas.put("status", false);
                apidatas.put("msg", paResponseCode+";"+requestForwardBiz.getForwardResult());
                Console.log(uuid+"返回值:"+apidatas.toString());
                return apidatas;
            }


        } catch (Exception e) {
            e.printStackTrace();
            Console.log(e.toString());
        }


        apidatas.put("data", json);
        apidatas.put("status", true);
        apidatas.put("msg", "转发成功");
        Console.log(uuid+"返回值:"+apidatas.toString());
        return apidatas;
    }

    public ReqOperateRequestEntity request2Entity() {
        ReqOperateRequestEntity var1 = new ReqOperateRequestEntity();

        try {
            int var2 = Util.getIntValue(Util.null2String(this.params.get("workflowId")));
            int var3 = Util.getIntValue(Util.null2String(this.params.get("requestId")));
            String var4 = Util.null2String(this.params.get("requestName"));
            int var5 = Util.getIntValue(Util.null2String(this.params.get("userId")));
            int var6 = Util.getIntValue(Util.null2String(this.params.get("forwardFlag")));
            String var7 = Util.null2String(this.params.get("forwardResourceIds"));
            String var8 = Util.null2String(this.params.get("mainData"));
            if (!"".equals(var8)) {
                try {
                    List var9 = JSONObject.parseArray(var8, WorkflowRequestTableField.class);
                    var1.setMainData(var9);
                } catch (Exception var16) {
                    var16.printStackTrace();
                }
            }

            String var18 = Util.null2String(this.params.get("detailData"));
            if (!"".equals(var18)) {
                try {
                 //   List var10 = JSONObject.parseArray(var18, WorkflowDetailTableInfoEntity.class);
                   // var1.setDetailData(var10);
                } catch (Exception var15) {
                    var15.printStackTrace();
                }
            }

            String var19 = Util.null2String(this.params.get("remark"));
            String var11 = Util.null2String(this.params.get("requestLevel"));
            String var12 = Util.null2String(this.params.get("otherParams"));
            if (!"".equals(var12)) {
                try {
                    Map var13 = (Map)JSONObject.parseObject(var12, Map.class);
                    var1.setOtherParams(var13);
                } catch (Exception var14) {
                    var14.printStackTrace();
                }
            }

            var1.setWorkflowId(var2);
            var1.setRequestId(var3);
            var1.setRequestName(var4);
            var1.setUserId(var5);
            var1.setRemark(var19);
            var1.setRequestLevel(var11);
            var1.setForwardFlag(var6);
            var1.setForwardResourceIds(var7);
            var1.setClientIp(Util.null2String(this.params.get("param_ip")));
            int var20 = Util.getIntValue(Util.null2String(this.params.get("submitNodeId")));
            if (var20 > 0 || FreeNodeBiz.isFreeNode(var20)) {
                var1.setSubmitNodeId(var20);
                var1.setEnableIntervenor("1".equals(Util.null2s(Util.null2String(this.params.get("enableIntervenor")), "1")));
                var1.setSignType(Util.getIntValue(Util.null2String(this.params.get("SignType")), 0));
                var1.setIntervenorid(Util.null2String(this.params.get("Intervenorid")));
            }
        } catch (Exception var17) {
            var17.printStackTrace();

        }

        return var1;
    }

}